package org.example.edusmart.utils;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GetTokenInfo {

    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 获取jwt token中的用户ID
     * @param request http请求头信息
     * @return token中的用户id
     */
    public String getUserIdByJwt(HttpServletRequest request) {
        //获取token
        String token = request.getHeader("token");
        // 解析token
        Claims claims = jwtUtils.parseJwt(token);
        return String.valueOf(claims.get("id"));
    }

}
